package com.gitee.aurora.server.service.impl;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.gitee.aurora.core.utils.ResultVO;
import com.gitee.aurora.core.utils.ResultVOUtils;
import com.gitee.aurora.server.dao.DataCheckDao;
import com.gitee.aurora.server.service.DataCheckService;
import com.gitee.aurora.comm.Constant;

import com.gitee.aurora.server.vo.DataCheckVo;

import com.gitee.aurora.server.vo.ResultCheckVo;
import com.gitee.aurora.server.vo.ResultSjzxData;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;

import javax.annotation.Resource;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;


/**
 * author: jiangfuxiao
 * Date:2022/9/2 11:34
 */
@Service("dataCheckServiceImpl")
public class DataCheckServiceImpl implements DataCheckService {

    @Resource(name = "dataCheckDaoImpl")
    private DataCheckDao dataCheckDao;

    @Resource
    private RestTemplate restTemplate;
    /**
     * 请求市级数据分析接口
     */
    @Value("${http.xjip}")
    private String  xjip;
    /**
     * 请求数据中心插入接口
     */
    @Value("${http.sjzxcr}")
    private String  sjzxcr;
    /**
     * 请求监督接口插入接口
     */
    @Value("${http.zfjdcr}")
    private String zfjdcr;
    /**
     * 请求省数据分析接口
     */
    @Value("${http.ssjzx}")
    private String ssjzx;
    /**
     * 省数据中心同步接口
     */
    @Value("${http.ssjzxtb}")
    private String ssjzxtb;


    @Override
    public ResultVO checkBaq(DataCheckVo checkVo) {
        if (checkVo == null){
            return ResultVOUtils.error("参数错误，接口调用失败");
        }
        //请求下级数据分析接口
        try {
            HttpHeaders headers = new HttpHeaders();
            HttpEntity<DataCheckVo> httpEntity = new HttpEntity<>(checkVo,headers);
            ResponseEntity<ResultVO> responseSjzx = restTemplate.exchange(xjip.replace(Constant.ip,checkVo.getSjzxIp().get(0)), HttpMethod.POST, httpEntity, ResultVO.class);
            return responseSjzx.getBody();
        }catch (Exception e){
            e.printStackTrace();
            return  ResultVOUtils.error("调用数据中心接口失败，请检查ip是否正确或网络是否正常");
        }
    }

    @Override
    public ResultVO queryListPage(DataCheckVo checkVo)  {
        if (null == checkVo || StringUtils.isBlank(checkVo.getJsonReslut()) || null == checkVo.getPageSize() || null == checkVo.getCurrentPage()){
            return ResultVOUtils.error("参数错误，接口调用失败");
        }
        ObjectMapper mapper = new ObjectMapper();
        List list = null;
        try {
            list = mapper.readValue(checkVo.getJsonReslut(),List.class) ;
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }

        int totalcount = list.size();

        int pagecount = 0;

        List subList;

        int m = totalcount % checkVo.getPageSize();

        if (m > 0) {

            pagecount = totalcount / checkVo.getPageSize() + 1;

        } else {

            pagecount = totalcount / checkVo.getPageSize();

        }

        if (m == 0) {

            subList = list.subList((checkVo.getCurrentPage() - 1) * checkVo.getPageSize(), checkVo.getPageSize() * (checkVo.getCurrentPage()));

        } else {

            if (checkVo.getCurrentPage() == pagecount) {

                subList = list.subList((checkVo.getCurrentPage() - 1) * checkVo.getPageSize(), totalcount);

            } else {

                subList = list.subList((checkVo.getCurrentPage() - 1) * checkVo.getPageSize(), checkVo.getPageSize() * (checkVo.getCurrentPage()));

            }

        }
        return ResultVOUtils.success(subList);
    }





    @Override
    public ResultVO insertData(DataCheckVo checkVo) {
        //请求数据中心插入接口
        String requestUrl =sjzxcr.replace(Constant.ip,checkVo.getSjzxIp().get(0));
        if (StringUtils.isNotBlank(checkVo.getZfjdIp())){
            requestUrl  = zfjdcr.replace(Constant.ip,checkVo.getZfjdIp());
        }
        HttpHeaders headers = new HttpHeaders();
        HttpEntity<DataCheckVo> httpEntity = new HttpEntity<>(checkVo,headers);
        URI url = UriComponentsBuilder.fromUriString(requestUrl).build().toUri();
        ResponseEntity<ResultVO> responseSjzx = restTemplate.postForEntity(url,httpEntity,ResultVO.class);
        return responseSjzx.getBody();
    }

    @Override
    public ResultVO checksszx(DataCheckVo checkVo) {
        if (checkVo == null){
            return ResultVOUtils.error("参数错误，接口调用失败");
        }
        if(!StringUtils.isNotEmpty(checkVo.getStSjzxIp())){
            return ResultVOUtils.error("参数错误，接口调用失败");
        }
        //请求省数据中心分析接口
        try {
            HttpHeaders headers = new HttpHeaders();
            HttpEntity<DataCheckVo> httpEntity = new HttpEntity<>(checkVo,headers);
            ResponseEntity<ResultVO> responseSjzx = restTemplate.exchange(ssjzx.replace(Constant.ip,checkVo.getStSjzxIp()), HttpMethod.POST, httpEntity, ResultVO.class);
            return responseSjzx.getBody();
        }catch (Exception e){
            e.printStackTrace();
            return  ResultVOUtils.error("调用省数据中心接口失败，请检查ip是否正确或网络是否正常");
        }
    }

    @Override
    public ResultVO insertSjzxData(ResultCheckVo resultCheckVo) {

        //省数据中心同步接口

        if(resultCheckVo==null){
            return ResultVOUtils.error("参数错误，接口调用失败");
        }
        String requestUrl =ssjzxtb.replace(Constant.ip,resultCheckVo.getStSjzxIp());
        HttpHeaders headers = new HttpHeaders();
        HttpEntity<ResultCheckVo> httpEntity = new HttpEntity<>(resultCheckVo,headers);
        URI url = UriComponentsBuilder.fromUriString(requestUrl).build().toUri();
        ResponseEntity<ResultVO> responseSjzx = restTemplate.postForEntity(url,httpEntity,ResultVO.class);
        return responseSjzx.getBody();
    }


}
